package com.wei.miaosha.controller;

import com.wei.miaosha.domain.User;
import com.wei.miaosha.rabbitmq.MQSender;
import com.wei.miaosha.redis.RedisService;
import com.wei.miaosha.redis.UserKey;
import com.wei.miaosha.result.CodeMsg;
import com.wei.miaosha.result.Result;
import com.wei.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by Weizichen on 2018/8/2.
 * desc:控制层sample
 */
@Controller
@RequestMapping("/demo")
public class SampleController {
    @Autowired
    UserService userService;

    @Autowired
    RedisService redisService;
    @Autowired
    MQSender mqSender;

    @RequestMapping("/mq")
    @ResponseBody
    public Result<String> mq() {
        mqSender.send("hello,mq");
        return Result.success("hello");
    }

//    @RequestMapping("/mq/topic")
//    @ResponseBody
//    public Result<String> topicMq() {
//        mqSender.sendTopic("hello,mq");
//        return Result.success("hello");
//    }
//
//    @RequestMapping("/mq/fanout")
//    @ResponseBody
//    public Result<String> fanoutMq() {
//        mqSender.sendFanout("hello,mq");
//        return Result.success("hello");
//    }
//
//    @RequestMapping("/mq/header")
//    @ResponseBody
//    public Result<String> headerMq() {
//        mqSender.sendHeader("hello,mq");
//        return Result.success("hello");
//    }

    @RequestMapping("/template")
    public String thymeleaf(Model model) {
        model.addAttribute("name", "tom");
        return "hello";
    }

    @RequestMapping("/error")
    @ResponseBody
    public Result<String> error() {
        return Result.error(CodeMsg.SESSION_ERROR);
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet() {
        User user = userService.getById(1);
        return Result.success(user);
    }

    @RequestMapping("/db/tx")
    public Result<Boolean> tx() {
        userService.tx();
        return Result.success(true);
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet() {
        User u1 = redisService.get(UserKey.getById, "1", User.class);
        return Result.success(u1);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Boolean> redisSet() {
//        redisService.set(UserKey.getById, "key2", "测试数据");
//        String s2 = redisService.get(UserKey.getById, "key2", String.class);
        User u = new User();
        u.setId(1);
        u.setName("神舟");
        redisService.set(UserKey.getById, "1", u);
        return Result.success(true);
    }
}
